<?php
namespace app\index\controller;



use basic\WapBasic;
use think\Cookie;
use think\Request;
use service\JsonService;
use app\index\model\user\User;
use think\Session;
use think\Url;

class Login extends WapBasic
{

    public function login()
    {
        if(Request::instance()->post()){
            $data= Request::instance()->post();
            if (empty($data['user_name'])) return JsonService::fail('请填写账号');
            if (empty($data['password'])) return JsonService::fail('请输入密码');
            $uid = User::GetRegUser($data['user_name'],$data['password']);
            if(empty($uid)){
                JsonService::fail('账号或密码错误');
            }
            Session::set('uid',$uid);
            return WapBasic::successful('登录成功',Url::build('user/index'));
        }
        return $this->fetch();
    }

    public function reg()
    {
        if(Request::instance()->post()){
            $data= Request::instance()->post();
            if (empty($data['user_name'])) return JsonService::fail('请填写账号');
            if (empty($data['one_password'])) return JsonService::fail('请输入密码');
            if (empty($data['two_password'])) return JsonService::fail('请再次输入密码');
            if($data['one_password'] <> $data['two_password']){
                return JsonService::fail('两次输入不一致');
            }
            if(User::AddUser($data)===false){
                return JsonService::fail(User::getErrorInfo('注册失败'));
            };

            return WapBasic::successful('注册成功，请登录',Url::build('login/login'));
        }
        return $this->fetch();
    }

    public function forget_password()
    {

        $get=[];
        $this->assign(['get'=>$get,]);
        return $this->fetch();
    }

    public function quit()
    {
        Session::delete('uid');
        return WapBasic::successful('你已成功退出',Url::build('login/login'));
    }
}
